package com.qiaofang.housewarehouse.facade.impl;

import com.qiaofang.housewarehouse.dao.utils.DateUtils;
import com.qiaofang.housewarehouse.service.HouseCollectionQualityService;
import com.qiaofang.housewarehouse.stub.constants.ResponseCode;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import com.qiaofang.housewarehouse.stub.facade.HouseCollectionQualityFacade;
import com.qiaofang.housewarehouse.stub.request.HouseCollectionQualityRequest;
import com.qiaofang.housewarehouse.stub.response.HouseCollectionQualityResponse;
import com.qiaofang.housewarehouse.stub.response.HouseCollectionQualitySourceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HouseCollectionQualityFacadeImpl implements HouseCollectionQualityFacade {
    private static final Logger logger = LoggerFactory.getLogger(HouseCollectionQualityFacadeImpl.class);

    @Resource
    private HouseCollectionQualityService houseCollectionQualityService;

    @Override
    public HouseCollectionQualityResponse getHouseCollectionQuality(@RequestBody @Valid HouseCollectionQualityRequest houseCollectionQualityRequest) {
        HouseCollectionQualityResponse response = new HouseCollectionQualityResponse();
        try {
            response.setData(houseCollectionQualityService.getHouseCollectionQuality(houseCollectionQualityRequest));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
        } catch (Throwable e) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("HouseCollectionQualityFacadeImpl.getHouseCollectionQuality error！", e);
        }
        return response;
    }

    @Override
    public HouseCollectionQualitySourceResponse listHouseCollectionQualitySource(@RequestBody @Valid HouseCollectionQualityRequest houseCollectionQualityRequest) {
        HouseCollectionQualitySourceResponse response = new HouseCollectionQualitySourceResponse();
        try {
            Map<String, String> paramMap = new HashMap<>();
            if(houseCollectionQualityRequest.getDataType().equals("出租")){
                paramMap.put("indexName", TableConstants.INDEX_RENTAL_HOUSE);
                paramMap.put("typeName", TableConstants.INDEX_TYPE_RENTAL_HOUSE);
            }else if (houseCollectionQualityRequest.getDataType().equals("出售")){
                paramMap.put("indexName", TableConstants.INDEX_SALE_HOUSE);
                paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
            }
            String startDate = DateUtils.parseDateYYYY_MM_DD(houseCollectionQualityRequest.getStartDate()) + " 00:00";
            String endDate = DateUtils.parseDateYYYY_MM_DD(houseCollectionQualityRequest.getEndDate()) + " 24:00";
            paramMap.put("dateRange",startDate + "~" + endDate);
            paramMap.put("city",houseCollectionQualityRequest.getCity());
            logger.info("paramMap：{}", paramMap.toString());
            response.setData(houseCollectionQualityService.listHouseCollectionQualitySource(paramMap));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
        } catch (Throwable e) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("HouseCollectionQualityFacadeImpl.getHouseCollectionQualitySource error！", e);
        }
        return response;
    }
}
